<template>
  <div class="app-container" style="height: 100%">
    <el-tabs v-model="queryParams.rdcType" class="tab-box" :class="{ disabled: disableAction }" @tab-click="handleQuery">
      <el-tab-pane label="集团检查" name="DEPT1" v-if="showBtns.jt" />
      <el-tab-pane label="集群检查" name="DEPT2" v-if="showBtns.jq" />
    </el-tabs>
    <el-form ref="queryForm" :model="queryParams" size="mini" inline style="white-space: nowrap; overflow-x: auto" @submit.native.prevent>
      <el-form-item prop="belongSystem">
        <el-select v-model="queryParams.belongSystem" placeholder="所属系统" clearable style="width: 120px">
          <el-option v-for="dict in dict.type.cq_pe_belong_system" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item prop="belongSystem">
        <el-select v-model="queryParams.isCount" placeholder="是否统计" clearable style="width: 120px">
          <el-option label="是" value="1" />
          <el-option label="否" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="showEditItem = true" v-if="showBtns.add">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" size="small" height="calc(100% - 100px)">
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="检查项编码" prop="chkitemCode" min-width="100" />
      <el-table-column label="检查项名称" prop="chkitemName" align="center" min-width="80" />
      <el-table-column label="扩展类型" show-overflow-tooltip align="center" min-width="100">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_business_type_expand" :value="row.businessTypeExpand" />
      </el-table-column>
      <el-table-column label="检查维度" align="center" min-width="80">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_dimension" :value="row.dimension" />
      </el-table-column>
      <el-table-column label="考核方式" min-width="100">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_ass_type" :value="row.dealType" />
      </el-table-column>
      <el-table-column label="进入条件" prop="entryConditionName" min-width="160" />
      <el-table-column label="退出条件" prop="exitConditionName" min-width="160" />
      <el-table-column label="频次" align="center" min-width="60">
        <template slot-scope="{ row }">
          <span>{{ row.frequency }}</span>
          <span>次</span>
          <span v-if="row.frequencyUnit && row.frequencyUnit !== 'EMPTY'">/{{ dict.label.cq_pe_frequency_unit[row.frequencyUnit] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属系统" prop="belongSystem" min-width="80">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_belong_system" :value="row.belongSystem" />
      </el-table-column>
      <el-table-column label="是否统计" align="center" width="80">
        <el-tag slot-scope="{ row }" size="small" :type="row.isCount ? 'primary' : 'info'">{{ row.isCount ? '是' : '否' }}</el-tag>
      </el-table-column>
      <el-table-column label="修改人" prop="updatorName" show-overflow-tooltip align="center" min-width="60" />
      <el-table-column label="修改时间" prop="updateTime" align="center" min-width="150">
        <span slot-scope="{ row }">{{ row.updateTime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="120">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditItem.open(row)" v-if="showBtns.edit">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-if="showBtns.remove">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <EditItem ref="EditItem" v-model="showEditItem" :list="list" @ok="getList" />
  </div>
</template>
<script>
// 项目抽球-铺排检查项配置
import auth from '@/plugins/auth'
import { CQ_EVAL_RDC_TYPE as RDC_TYPE } from '@/utils/baseData'
import { listPeChkCfg, delPeChkCfg } from '@/api/cqPe/bsChkitem'
import EditItem from './components/EditPeChkCfg'
export default {
  name: 'PeChkCfg',
  components: { EditItem },
  dicts: ['cq_pe_business_type_expand', 'cq_pe_dimension', 'cq_pe_ass_type', 'cq_pe_frequency_unit', 'cq_pe_belong_system'],
  data() {
    return {
      listLoading: false,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 1e5,
        rdcType: '',
        belongSystem: '',
        isCount: ''
      },
      showEditItem: false
    }
  },
  computed: {
    showBtns() {
      // 操作按钮权限
      const rdcType = this.queryParams.rdcType.toLowerCase()
      const permCode = `cqPe:peChkCfg:${rdcType}`
      const query = auth.hasPermi(`${permCode}:query`)
      const add = auth.hasPermi(`${permCode}:add`)
      const edit = auth.hasPermi(`${permCode}:edit`)
      const remove = auth.hasPermi(`${permCode}:remove`)

      const jt = auth.hasPermi('cqPe:peChkCfg:dept1:query')
      const jq = auth.hasPermi('cqPe:peChkCfg:dept2:query')

      return { query, add, edit, remove, jt, jq }
    },
    disableAction() {
      return this.listLoading
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      // 判断检查单位(集团/集群)权限
      const permCode = 'cqPe:peChkCfg'
      const jt = auth.hasPermi(`${permCode}:dept1:query`)
      const jq = auth.hasPermi(`${permCode}:dept2:query`)
      if (jt && jq) this.queryParams.rdcType = RDC_TYPE.DEPT1
      else if (jt) this.queryParams.rdcType = RDC_TYPE.DEPT1
      else if (jq) this.queryParams.rdcType = RDC_TYPE.DEPT2
      else return

      this.getList()
    },
    async getList() {
      const { showBtns } = this
      if (!showBtns.query) return (this.list = [])

      this.listLoading = true
      const res = await listPeChkCfg(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return

      const data = res.rows || []
      data.map(i => {
        i.removeLoading = false
      })
      this.list = data
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      // 注：部分字段重置
      this.queryParams.belongSystem = ''
      this.queryParams.isCount = ''
      this.handleQuery()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除：${item.chkitemName}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delPeChkCfg(item.rpChkitemId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
